package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.model.UserBindCards;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerHolder;
import com.cardbaobao.cardbabyclient.ui.helper.BankCardBgHelper;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Jiangzx on 2017/9/14 10:01.
 */

public class CreditCardAdapter  extends CommonRecyclerAdapter<CreditCardAdapter.CreditCardItem>{
    public static final int TYPE_ADD=0;
    public static final int TYPE_NO_CARD=1;
    public static final int TYPE_ITEM=2;

    public interface CreditCardAction{
        int ACTION_UNBIND=1;
        int ACTION_ADD_CARD=2;
        void onCreditCardAction(int action,UserBindCards.CreditCard card);
    }

    private CreditCardAction listener;
    public void setCreditCardAction(CreditCardAction listener){
        this.listener=listener;
    }

    public CreditCardAdapter(Context context, List<CreditCardItem> data) {
        super(context, data, R.layout.item_credit_card);
    }

    @Override
    public void convert(CommonRecyclerHolder holder, final CreditCardItem creditCardItem) {
        UserBindCards.CreditCard card=creditCardItem.getItem();
        holder.itemView.setBackgroundResource(
                BankCardBgHelper.getBankCardBgRes(card.getBack_color()));
        ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+card.getPicture(),
                (CircleImageView) holder.getView(R.id.civ_logo),
                R.mipmap.img_default_square);
        holder.setTextViewText(R.id.tv_bank,card.getCredit_card_name());
        String cardNumber=card.getCredit_card_no();
        StringBuilder cardNoSB=new StringBuilder(Constant.CARD_PREFIX);
        int length=cardNumber.length();
        for(int i=length-4;i<length;i++){
            cardNoSB.append(cardNumber.charAt(i));
        }
        holder.setTextViewText(R.id.tv_card,cardNoSB.toString());
        holder.getView(R.id.tv_unbind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onCreditCardAction(CreditCardAction.ACTION_UNBIND,
                            creditCardItem.getItem());
                }
            }
        });
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType=getItemViewType(position);
        switch (viewType){
            case TYPE_ADD:
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(listener!=null){
                            listener.onCreditCardAction(CreditCardAction.ACTION_ADD_CARD,null);
                        }
                    }
                });
                break;
            case TYPE_NO_CARD:
                break;
            case TYPE_ITEM:
                convert((CommonRecyclerHolder) holder,mData.get(position));
                break;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context=parent.getContext();
        LayoutInflater inflater=LayoutInflater.from(context);
        if(viewType==TYPE_ADD){
            View view=inflater.inflate(R.layout.item_dash_add,parent,false);
            ((TextView)view.findViewById(R.id.tv_add_card)).setText("添加信用卡");
            return new RecyclerView.ViewHolder(view) {
            };
        }
        if(viewType==TYPE_NO_CARD){
            LinearLayout container=
                    new LinearLayout(parent.getContext());
            LinearLayout.LayoutParams layoutParams=
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
            container.setGravity(Gravity.CENTER);
            container.setLayoutParams(layoutParams);
            TextView textView=new TextView(context);
            final int width= ScreenUtil.getScreenWidth()-DensityUtil.dp2px(16)*2;
            RecyclerView.LayoutParams params=
                    new RecyclerView.LayoutParams(width,
                            RecyclerView.LayoutParams.WRAP_CONTENT);
            textView.setLayoutParams(params);
            textView.setTextColor(0XFFAFAFAF);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
            textView.setText("绑定信用卡付款更便捷");
            textView.setCompoundDrawablePadding(DensityUtil.dp2px(15));
            textView.setCompoundDrawablesWithIntrinsicBounds(null,
                    ContextCompat.getDrawable(context,R.mipmap.img_credit_card),null,null);
            int padding11=DensityUtil.dp2px(11);
            textView.setPadding(0,padding11,0,padding11);
            textView.setGravity(Gravity.CENTER);
            container.addView(textView);
            return new RecyclerView.ViewHolder(container) {
            };
        }
        if(viewType==TYPE_ITEM){
            View view=inflater.inflate(R.layout.item_credit_card,parent,false);
            return new CommonRecyclerHolder(view);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getType();
    }

    public static class CreditCardItem{
        public CreditCardItem() {
        }

        public CreditCardItem(int type, UserBindCards.CreditCard item) {
            this.type = type;
            this.item = item;
        }

        private int type;
        private UserBindCards.CreditCard item;

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public UserBindCards.CreditCard getItem() {
            return item;
        }

        public void setItem(UserBindCards.CreditCard item) {
            this.item = item;
        }
    }
}
